#include <iostream>
#include <cstring>
using namespace std;
const int N=2010;
int T,n,a[N],dp[N][N][2],ans;
void dfs(int x,int s,int t,int ss,int f)
{
    int tp,s1,t1;
    if(f==1) tp=t;
    else tp=s;
    if(dp[x][tp][f]>ss) return;
    dp[x][tp][f]=ss;
    if(x==n+1){
            ans=max(ans,ss);
        return;
    }
    dfs(x+1,x,t,ss+(a[x]==a[s])*a[x],1);
    dfs(x+1,s,x,ss+(a[x]==a[t])*a[x],2);
}
int main()
{
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    scanf("%d",&T);
    while(T--){
            memset(dp,0,sizeof(dp));
            ans=0;
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
        }
        dfs(1,0,0,0,0);
        printf("%d\n",ans);

        /*else{
             for(int i=1;i<=n;i++){
                 for(int j=1;j<i;j++){
                        int tp2=0;
                        for(int k=j+1;k<i;k++)
                            if(a[k]==a[k-1]) tp2+=a[k];
                        for(int k=0;k<j;k++){
                            if(a[i]==a[j]){int tp;
                                    if(j==i-1) tp=k;
                                    else tp=i-1;
                                dp[i][tp][0]=max(dp[i][k][0],dp[j][k][0]+a[i]+a[j+1]*(a[j+1]==a[k])+tp2);
                                dp[i][tp][1]=max(dp[i][k][1],dp[j][k][1]+a[i]+a[j+1]*(a[j+1]==a[k])+tp2);
                                ans=max(ans,max(dp[i][tp][0],dp[i][tp][1]));
                            }
                            dp[i][k][0]=max(dp[i][k][0],max(dp[i-1][i-1][1],dp[i-1][k][0]+a[i]*(a[i]==a[i-1])));
                            dp[i][k][1]=max(dp[i][k][1],max(dp[i-1][i-1][0],dp[i-1][k][1]+a[i]*(a[i]==a[i-1])));
                        }
            }
            }
            printf("%d\n",ans);
        }
*/
    }
    return 0;
}
